package com.jereh.cloudmusic.actvity;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;

import com.jereh.cloudmusic.R;
import com.jereh.cloudmusic.adapter.FragmentViewPagerAdapter;
import com.jereh.cloudmusic.fragment.DiscoverFragment;
import com.jereh.cloudmusic.fragment.FriendsFragment;
import com.jereh.cloudmusic.fragment.MusicFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private TabLayout tab;
    private ViewPager pager;
    private PagerAdapter adapter;
    private int[] resId={R.drawable.actionbar_discover_selector,R.drawable.actionbar_music_selector,R.drawable.actionbar_friends_selector};
    private List<Fragment> fragments=new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViews();
    }

    private void initViews(){
        tab=(TabLayout)findViewById(R.id.tab);
        pager=(ViewPager)findViewById(R.id.pager);
        fragments.add(new DiscoverFragment());
        fragments.add(new MusicFragment());
        fragments.add(new FriendsFragment());
        adapter=new FragmentViewPagerAdapter(getSupportFragmentManager(),fragments);
        pager.setAdapter(adapter);
        tab.setupWithViewPager(pager);
        initUpIcons();
    }

    private void initUpIcons(){
        tab.getTabAt(0).setCustomView(getTabView(0));
        tab.getTabAt(1).setCustomView(getTabView(1));
        tab.getTabAt(2).setCustomView(getTabView(2));
    }

    private View getTabView(int position) {
        ImageView view=(ImageView)getLayoutInflater().inflate(R.layout.tab_custom_view,null);
        switch (position) {
            case 0:
                view.setImageResource(R.drawable.actionbar_discover_selector);
                break;
            case 1:
                view.setImageResource(R.drawable.actionbar_music_selector);
                break;
            case 2:
                view.setImageResource(R.drawable.actionbar_friends_selector);
                break;
        }
        return view;
    }
}
